package wu.controller;

import org.apache.ibatis.session.SqlSession;
import wu.dao.ReadTxtDao;
import wu.mapper.CreateTableMapper;
import wu.mapper.UserNiuMapper;
import wu.pojo.User_niu;
import wu.service.DownloadHTML;
import wu.util.MyBatisUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;


public class insert_niuRound_num {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
//        System.out.println("请输入要插入的周赛周数：");
//        int num = sc.nextInt();
        System.out.println("请输入要本周周赛的比赛id：");
        int id = sc.nextInt();
        String url = "https://ac.nowcoder.com/acm/contest/" + id + "#rank/%22searchUserName%22%3A%22%E6%B2%B3%E6%B1%A0%E5%AD%A6%E9%99%A2%22";

        System.out.println("正在下载网站信息  ****************************************");
        List<String> urllist = DownloadHTML.NiuUsersService(url);
        String htmlString = urllist.get(0);
        int num = Integer.parseInt(urllist.get(1));
        System.out.println("成功下载网站信息  ****************************************");

        System.out.println("正在创建文件  ****************************************");
        String path = "src/main/resources/niu_number_txtfile/niu_" + num + "_txt";
        try {
            File file = new File(path);
            System.out.println("niu_" + num + "_txt");
            System.out.println("成功创建文件  ****************************************");
            if (!file.exists()) {
                if (file.createNewFile()) {
                    System.out.println("File created: " + file.getName());
                } else {
                    System.out.println("Failed to create file.");
                }
            }

            System.out.println("正在写入文件  ****************************************");
            FileWriter writer = new FileWriter(file);
            writer.write(htmlString);
            writer.close();
            System.out.println("已成功写入文件  ****************************************");
        } catch (IOException e) {
            System.out.println("写入文件出现了一个错误  ！！！！！！！！！！！！！！！！！！！！！！！！");
            e.printStackTrace();
        }

        SqlSession sqlSession = MyBatisUtils.getSession();
        List<String> readlist = ReadTxtDao.readfile(num);
        System.out.println("输出读取的txt文件内容  ****************************************");
        for (String content : readlist) {
            System.out.println(content);
        }
        System.out.println("输出完成        *********************************************");

        System.out.println("传入getUser");
        List<User_niu> userNiuslist = ReadTxtDao.getniu_User(num, readlist);
        System.out.println("输出获取的list  *********************************************");
        for (User_niu user_niu : userNiuslist) {
            System.out.println(user_niu.toString());
        }
        System.out.println("输出完成        *********************************************");

        System.out.println("正在建表");
        Map<String, String> params = new HashMap<>();
        params.put("tableName", ("user_table_" + num));
        CreateTableMapper createTableMapper = sqlSession.getMapper(CreateTableMapper.class);
        createTableMapper.createtable222(params);
        System.out.println("建表完成");

        System.out.println("正在插入数据");
        UserNiuMapper mapper = sqlSession.getMapper(UserNiuMapper.class);
        mapper.insertUserNiuList("user_table_" + num, userNiuslist);
        System.out.println("插入完成");
        sqlSession.commit();
        sqlSession.close();
    }
}
